package au.com.centrumsystems.hudson.plugin.buildpipeline;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;

import java.util.List;

import au.com.centrumsystems.hudson.plugin.util.BuildUtil;
import au.com.centrumsystems.hudson.plugin.util.ProjectUtil;

public class BuildGrid extends GridForm<RevisionHeader, BuildForm> {

    public static class Builder
    extends GridForm.Builder<RevisionHeader, BuildForm> {

        private final RevisionHeader.Factory headerFactory;
        private final Provider<BuildForm.Builder> builderProvider;

        @Inject
        public Builder(RevisionHeader.Factory headerFactory, BuildGrid.Factory factory, Provider<BuildForm.Builder> builderProvider) {
            super(factory);
            this.headerFactory = headerFactory;
            this.builderProvider = builderProvider;
        }

        public Builder setRoot(AbstractBuild<?,?> currentBuild, AbstractProject<?,?> project) {
            setHeader(headerFactory.create(currentBuild));

            return (BuildGrid.Builder) 
                setRoot(
                    builderProvider.get()
                        .setBuild(currentBuild)
                        .setProject(project)
                        .build()
                );
        }
    }

    public interface Factory extends GridForm.Factory<RevisionHeader, BuildForm> {

        BuildGrid create(RevisionHeader header, List<ColumnForm<BuildForm>> columns);
    }

    @Inject
    public BuildGrid(@Assisted RevisionHeader header, @Assisted List<ColumnForm<BuildForm>> columns) {
        super(header, columns, "build-pipeline");
    }
}

